package uk.ac.aber.paws.client.system;

import uk.ac.aber.paws.client.system.NativeCalls.User32;


import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.LPARAM;
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinDef.WPARAM;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.win32.W32APIOptions;
import com.sun.jna.win32.StdCallLibrary.StdCallCallback;

/**
 * @author Rob Johnson, Aberystwyth University
 * @email rob.johnson@aber.ac.uk
 * @date 16th August 2011
 * 
 * ShutdownDetect.java
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class ShutdownDetect {

	    public static int GWL_WNDPROC = -4;

	public interface MyUser32 extends User32 {
    	 
    	 public static final MyUser32 MYINSTANCE = (MyUser32) Native.loadLibrary("user32", MyUser32.class, W32APIOptions.UNICODE_OPTIONS);


 	    interface WindowProc extends StdCallCallback {
 	        LRESULT callback(HWND hWnd, int uMsg, WPARAM wParam, LPARAM lParam);
 	    }

 	    int SetWindowLong(HWND hWnd, int nIndex, int dwNewLong);

 	    int SetWindowLong(HWND hWnd, int nIndex, WindowProc dwNewLong);
     }
 
	public interface MyWinUser extends WinUser {
	    /**
	     * Notifies an application of a change to the hardware configuration of a device or the computer.
	     */
	    public static final int WM_DEVICECHANGE = 0x0219;
	    public static final int WM_QUERYENDSESSION = 0x11;

	}
	
	//Create the callback interface 
	public interface MyListener extends StdCallCallback {

	    public LRESULT callback(HWND hWnd, int uMsg, WPARAM uParam, LPARAM lParam);
	}

	public MyListener listener = new MyListener()
	{
	    public LRESULT callback(HWND hWnd, int uMsg, WPARAM uParam, LPARAM lParam)
	    {
	        if (uMsg == MyWinUser.WM_QUERYENDSESSION)
	        {
	            // TODO Check If my device was attached or detached
	            return new LRESULT(1);
	        }
	        return new LRESULT(0);
	    }
	};
	
}
